'use strict';

import $storage from "@/store/storage/storage";

// 设置的唯一KEY
export const GLOBAL_SETTING_KEY = 'GLOBAL_SETTING_KEY';

/**
 * 设置全局缓存参数
 *
 * @param store
 * @param setting
 */
export const setGlobalStorageSetting = (store, setting) => {
  // 更新状态管理中的每页显示数量
  if (setting && setting.pageSize && setting.pageSize !== store.state.pageSize) {
    store.commit('updatePageSize', setting.pageSize);
  }
  // 保存本地缓存
  $storage.setItem(GLOBAL_SETTING_KEY, setting);
  console.info('设置全局缓存参数: ', setting);
};

/**
 * 加载全局缓存设置参数
 *
 * @param store
 */
export const loadGlobalStorageSetting = store => {
  // 从本地缓存取出
  let setting = $storage.getItem(GLOBAL_SETTING_KEY);
  console.info('加载全局缓存设置参数: ', setting);
  // 更新状态管理中的每页显示数量
  if (setting && setting.pageSize) {
    store.commit('updatePageSize', setting.pageSize);
  }
};

// 导出默认对象
export default {
  GLOBAL_SETTING_KEY,
  setGlobalStorageSetting,
  loadGlobalStorageSetting
};
